/*
 * Created on 08-mar-2005
 *
 */
package guru.merlin.server;

import guru.merlin.core.Envelope;

import java.util.Enumeration;

import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;

/**
 * @author
 *
 */
public class QueueProcessor{
    String                  queueName = null;
    //QueueConnectionFactory  queueConnectionFactory = null;
    //eueConnection         queueConnection = null;
    //QueueSession            queueSession = null;
    //Queue                   queue = null;
    
    
    public static void main (String[] args) {
        if(args.length!=3){
            System.err.println ("error: syntax: provide queue name");
            return;
        }
        try{
            new QueueProcessor (args[0],args[1],args[2],args[3]);
        } catch(Throwable t){
            t.printStackTrace ();
        }
        
        
    }
    
    QueueProcessor (String queueName, String newQueue, String passport, String cmd) throws Exception{
        QueueConnection queueConnection = null;
        Queue queue = null;
        QueueSession queueSession = null;
        QueueBrowser queueBrowser = null;
        
        QueueConnection nqueueConnection = null;
        Queue nqueue = null;
        QueueSession nqueueSession = null;
        QueueBrowser nqueueBrowser = null;
        if (queueName!=null) {
            System.out.println ("Inicio");
            System.out.println ("Conectado a '"+queueName+"'");
            queueConnection = getQueueConection (queueName);
            queue = getQueue (queueConnection);
            queueSession = getQueueSession (queueConnection);
            queueBrowser = queueSession.createBrowser (queue);
            System.out.println ("Cargado "+queue);
        }
        if (newQueue!=null) {
            System.out.println ("Conectado a '"+newQueue+"'");
            nqueueConnection = getQueueConection (newQueue);
            nqueue = getQueue (nqueueConnection);
            nqueueSession = getQueueSession (nqueueConnection);
            nqueueBrowser = nqueueSession.createBrowser (nqueue);
            System.out.println ("Cargado "+queue);
        }
        for (Enumeration e = queueBrowser.getEnumeration () ; e.hasMoreElements () ;) {
            Object o=e.nextElement ();
            try{
                if (o instanceof ObjectMessage) {
                    ObjectMessage  msg = (ObjectMessage) o;
                    
                    Envelope envelope=(Envelope)msg.getObject ();
                    if (envelope==null) {
                        System.out.println ("ES NULO");
                    }
                    System.out.println ("---------------------------------------------------------------");
                    
                    String envContent = envelope.toString ();
                    System.out.println ("Envelope:"+envContent);
                    /*
                    if (!envelope.getOrigin ().getVariable ().equalsIgnoreCase (passportFilter)){
                        put (envelope,nqueueSession,nqueue);
                    }
                    */
                    if(envelope.getDestination ()!=null){
                        System.out.println ("=destination=> "+envelope.getDestination ().getClass ().getName ());
                    }
                    
                    if(envelope.getOrigin ()!=null){
                        System.out.println ("=origin=> "+envelope.getOrigin ().getClass ().getName ());
                    }
                    
                    System.out.println ("OriginRoute:"+envelope.getOriginRoute ());
                    
                    System.out.println ("DestinationRoute:"+envelope.getDestinationRoute ());
                    
                    if(envelope.getComment ()!=null){
                        System.out.println ("=comment=> No 'comment' in Envelope");
                    } else{
                        System.out.println ("=comment=> "+envelope.getComment ());
                    }
                    if(envelope.getThrowable ()==null){
                        System.out.println ("=throwable=> No throwable in Envelope");
                    } else{
                        System.out.println ("=throwable=>"+envelope.getThrowable ().getClass ().getName ());
                        System.out.println ("=throwable=>"+envelope.getThrowable ().getLocalizedMessage ());
                    }
                    
                } else {
                    throw new InternalError ("onMessage(1)@JMSQueue: message was not an instance of ObjectMessage");
                }
            } catch(Throwable t){
                System.err.println ("CESAR:ERROR:"+t);
                t.printStackTrace ();
            }
            
        }
        
        
        
        
        System.out.println ("Fin");
        
        queueConnection.close ();
        nqueueConnection.close ();
        
        
    }
    
    private QueueConnection getQueueConection (String qName) throws Exception{
        this.queueName=qName;
        QueueConnectionFactory queueConnectionFactory = new com.sun.messaging.QueueConnectionFactory ();
        QueueConnection queueConnection = queueConnectionFactory.createQueueConnection ();
        return queueConnection;
    }
    
    private QueueSession getQueueSession (QueueConnection queueConnection) throws Exception  {
        return queueConnection.createQueueSession (false,Session.AUTO_ACKNOWLEDGE);
    }
    
    private Queue getQueue (QueueConnection queueConnection) throws Exception  {
        Queue queue = new com.sun.messaging.Queue (queueName);
        return queue;
    }
    
    public void put (Envelope envelope, QueueSession queueSession, Queue queue) throws Exception {
        ObjectMessage message = null;
        try {
            String msg = "";
            message = queueSession.createObjectMessage ();
            message.setObject (envelope);
            msg = "[send@JMSQueue] Sending message: " + envelope.toString ();
            System.out.println (msg);
            QueueSender queueSender = queueSession.createSender (queue);
            queueSender.send (message);
        }  finally{
            
        }
    }
}
